<?php  if (!defined('PATH_HD')) exit('No direct script access allowed');

class IndexControl extends Control{
	// 判断是否已安装过
	function __auto() {
        if (is_file("./lock.php")) {
            echo "请删除lock.php文件后再安装";
            exit;
        }
    }
    function index(){
        $_SESSION['step'] = 1;
        $this->display();
    }
    // 第二步
    function step2() {
        if ($_SESSION['step'] !== 1) {
            go("index");
        }
        if($_POST['agree']!='on'){
            go("index");
        }
        $_SESSION['step'] = 2;
        $functions = array(
            "version" => PHP_VERSION > 5.1,
            "gd" => extension_exists("GD"),
            "imagejpeg" => function_exists("imagejpeg"),
            "imagepng" => function_exists("imagepng"),
        );
        $root = dirname(PATH_ROOT);
        $dirs = array(
            "root" => is_writable($root),
            "config" => is_writable($root . '/config'),
            "temp" => is_writable($root . '/temp'),
            "h" => is_writable($root . '/h')
        );
        $this->assign("check", array(
            "functions" => $functions,
            "dirs" => $dirs
                )
        );
        $this->display();
    }
    //第三步设置数据库
    public function step3(){
        if ($_SESSION['step'] < 2){
            go("step2");
        }
        $_SESSION['step'] = 3;
        $this->display();
    }
    public function step4(){
        if ($_SESSION['step'] < 3){
            go("step3");
            $_SESSION['step'] = 4;
        }
        $link = @mysql_connect($_POST['DB_HOST'] . ":" . $_POST['DB_PORT'], $_POST['DB_USER'], $_POST['DB_PASSWORD']);
        $select_db = @mysql_select_db($_POST['DB_DATABASE']);
        if (!$link || !$select_db) {
            $this->error("数据库配置错误");
        }

        $str = "<?php \n";
        $str.='if (!defined("PATH_HD")) exit("No direct script access allowed");';
        $str.="\n return " . var_export($_POST, true) . "\n?>";
        if (!@file_put_contents("../config/db.php", $str)) {
            $this->error("config目录不可写，请修改权限");
        }
        go("recovery");
    }
    function recovery() {
        C(include "../config/db.php");
        Backup::recovery(array(
            "dir" => "tables",
            "url" => "recoverySuccess",
            )
        );
    }
    function recoverySuccess() {
        touch("lock.php");
        $root = dirname(__ROOT__);
        $this->assign('root',$root);
        $this->display('step4.html');

        // echo "<div>
        //     <h2>安装完成</h2>
        //     <a href='$root'>进入首页</a>
        //     <a href='$root/admin.php'>登录后台</a>
        // </div>";
        // session_unset();
        // session_destroy();
        // exit;
    }
}
?>